﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using Entity;
using System.Collections.ObjectModel;

namespace Utilities
{
    public class PropertyQuery
    {
        private const String FGDB_WORKSPACE = @"E:\VirtualCampus\ArcGlobeData\ZhiMingBuilding\Campus3D\BuildingWays.gdb";
        private String INPUT_FC = "TClassRoom";
        public String InputFC
        {
            get { return this.INPUT_FC; }
            set { this.INPUT_FC = value; }
        }
        public EClassRoom Query(int key)
        {
            this.INPUT_FC = "TClassRoom";
            IWorkspaceFactory workspaceFactory = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")) as IWorkspaceFactory;
            IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(FGDB_WORKSPACE, 0) as IFeatureWorkspace;
            IFeatureClass inputIncidentsFClass = featureWorkspace.OpenFeatureClass(INPUT_FC);

            IQueryFilter pQueryFilter;
            IFeatureCursor pFeatureCursor;
            IFeature pFeature;
            EClassRoom cr=null;            
            
            pQueryFilter = new QueryFilterClass();
            pQueryFilter.WhereClause = "CRCode=" + key;

            pFeatureCursor = inputIncidentsFClass.Search(pQueryFilter, false);
            pFeature = pFeatureCursor.NextFeature();
            if (pFeature != null)
            {
                int crCode = Convert.ToInt32(pFeature.get_Value(3));
                double xLon = Convert.ToDouble(pFeature.get_Value(6));
                double yLat = Convert.ToDouble(pFeature.get_Value(7));
                double zAlt = Convert.ToDouble(pFeature.get_Value(5));
                int floor = Convert.ToInt32(pFeature.get_Value(4));

                IPoint gcsPoint = EsriUtility.PCS2GCS(xLon, yLat);
                cr = new EClassRoom(crCode, gcsPoint.X, gcsPoint.Y, zAlt, floor);  
            }
            return cr;
        }
        public IPoint QueryPoint(int key,string LayerName,string PropertyName)
        {
            this.INPUT_FC = LayerName;
            IWorkspaceFactory workspaceFactory = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")) as IWorkspaceFactory;
            IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(FGDB_WORKSPACE, 0) as IFeatureWorkspace;
            IFeatureClass inputIncidentsFClass = featureWorkspace.OpenFeatureClass(INPUT_FC);
                        
            IQueryFilter pQueryFilter;
            IFeatureCursor pFeatureCursor;
            IFeature pFeature;
            IPoint outPoint=new PointClass();

           
            pQueryFilter = new QueryFilterClass();
            pQueryFilter.WhereClause = PropertyName+"=" + key;

            pFeatureCursor = inputIncidentsFClass.Search(pQueryFilter, false);
            pFeature = pFeatureCursor.NextFeature();
            if (pFeature != null)
            {
                outPoint = pFeature.Shape as IPoint;
            }
            return outPoint;
        }
        public List<int> QueryAllCR()
        {
            List<int> crCodes = new List<int>();
            IWorkspaceFactory workspaceFactory = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")) as IWorkspaceFactory;
            IFeatureWorkspace featureWorkspace = workspaceFactory.OpenFromFile(FGDB_WORKSPACE, 0) as IFeatureWorkspace;
            IFeatureClass inputIncidentsFClass = featureWorkspace.OpenFeatureClass(INPUT_FC);

            IFeatureCursor pFeatureCursor;
            IFeature pFeature;

            pFeatureCursor = inputIncidentsFClass.Search(null, false);
            
            pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                int crCode = Convert.ToInt32(pFeature.get_Value(3));
                crCodes.Add(crCode);
                pFeature = pFeatureCursor.NextFeature();
            }
            return crCodes;
        }
    }
}
